package com.sinosoft.hydra.um.services.component.dataimport.validators;

import com.sinosoft.hydra.um.services.component.dataimport.resolvers.ValidateResult;

/**
 * Created by Administrator on 2015/1/10.
 */
public class MaxSizeValidator extends Validator {

    private int maxNumber;
    private String errorMsg;

    public MaxSizeValidator(int maxNumber) {
        this.maxNumber = maxNumber;
    }

    public MaxSizeValidator(int maxNumber, String errorMsg) {
        this.maxNumber = maxNumber;
        this.errorMsg = errorMsg;
    }

    public ValidateResult validate(Object obj) {
        if(obj == null){
            return ValidateResult.PASS();
        }
        try {
            String ss = (String)obj;
            if(ss.length() > maxNumber){
                if(errorMsg != null){
                    return ValidateResult.ERROR(errorMsg);
                }
                return ValidateResult.ERROR("数据的最大长度不能超过" + maxNumber + "个字符");
            }
        }catch (Exception ex){
            throw new ValidateException();
        }

        return ValidateResult.PASS();
    }
}
